package com.geng.demo.config;

import com.geng.demo.util.ParamUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.Map;

@Component
@RestControllerAdvice
public class GlobalExceptionHandle {

    @ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class})
    public Map<String, Object> paramCheckError3(MethodArgumentNotValidException e) {
        StringBuilder sb = new StringBuilder();
        e.getBindingResult().getAllErrors()
                .forEach(x -> sb.append(x.getDefaultMessage()).append(";"));
        Map<String, Object> resultMap = ParamUtil.returnErrorMessage(sb.toString());
        return resultMap;
    }

}
